package dao

import (
	"fmt"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// SystemSmsLogOption SystemSmsLog 选项
type SystemSmsLogOption option

// SystemSmsLogOptionWithLock 选项：加锁查询
func SystemSmsLogOptionWithLock() SystemSmsLogOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Clauses(clause.Locking{Strength: SelectForUpdate}), nil
	}
}

// SystemSmsLogOptionWithOrderDefault 选项：默认排序
func SystemSmsLogOptionWithOrderDefault() SystemSmsLogOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Order(fmt.Sprintf("%s.create_time DESC", (&model.SystemSmsLog{}).TableName())), nil
	}
}

// SystemSmsLogOptionWithId 选项：根据id查询
func SystemSmsLogOptionWithId(id ...string) SystemSmsLogOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		if 0 == len(id) {
			return nil, errors.ErrorRecordNotFound("IID_AO.SSLOWI_ID.330357")
		}

		return session.Where(fmt.Sprintf("%s.id IN ?", (&model.SystemSmsLog{}).TableName()), id), nil
	}
}

func SystemSmsLogOptionWithSendType(sendType model.SystemSmsLogSendType) SystemSmsLogOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.send_type = ?", (&model.SystemSmsLog{}).TableName()), sendType), nil
	}
}

func SystemSmsLogOptionWithSendTimeEqualOrLessThan(sendTime int64) SystemSmsLogOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.send_time <= ?", (&model.SystemSmsLog{}).TableName()), sendTime), nil
	}
}
